package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.context.BaseContext;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.dto.PageQueryDTO;
import com.heima.pojo.dto.VmTypeDTO;
import com.heima.pojo.entity.VmType;
import com.heima.result.PageResult;
import com.heima.service.VmTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VmTypeServiceImpl implements VmTypeService {

    @Autowired
    VmTypeMapper vmTypeMapper;

    /**
     * 设备搜索
     */
    @Override
    public PageResult<VmType> selectType(PageQueryDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<VmType> page = vmTypeMapper.selectType(dto.getName());
        return new PageResult<>(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    /**
     * 新增设备类型
     */
    @Override
    public boolean add(VmTypeDTO dto) {
        VmType vmType = new VmType();
        BeanUtils.copyProperties(dto, vmType);
        int result = vmTypeMapper.add(vmType);
        return result > 0;
    }

    /**
     * 修改设备类型
     */
    @Override
    public boolean update(Integer typeId, VmTypeDTO dto) {
        VmType vmType = new VmType();
        vmType.setTypeId(typeId);
        BeanUtils.copyProperties(dto, vmType);
        int result = vmTypeMapper.update(vmType);
        return result > 0;
    }

    /**
     * 删除设备类型
     */
    @Override
    public boolean delete(Integer typeId) {
        int result = vmTypeMapper.delete(typeId);
        return result>0;
    }
}
